package com.naml.b_sellticket;

/**
 * @author marin
 * @version 1.0
 * @date 2025/4/26 17:24
 * @desc 使用实现Runnable接口实现窗口卖票
 */
@SuppressWarnings("all")
public class ThreadSellTicket02 {
    public static void main(String[] args) {
        SellTicket02 sellTicket02 = new SellTicket02();
        Thread thread1 = new Thread(sellTicket02);
        Thread thread2 = new Thread(sellTicket02);
        Thread thread3 = new Thread(sellTicket02);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

@SuppressWarnings("all")
class SellTicket02 implements Runnable {
    //由于实现方式，只会创建一个对象，所以不需要static修饰
    private int count = 100;

    @Override
    public void run() {
        while(true){
            if (count <= 0) {
                break;
            }
            try {
                Thread.sleep(500);
                System.out.println("窗口"+Thread.currentThread().getName() +"：卖票，剩余张数："+(--count));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
